package com.processor;

import com.entity.Student;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.PropertyValue;
import org.springframework.beans.PropertyValues;
import org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessor;

import java.beans.PropertyDescriptor;

public class CustomInstatiationAwareBeanPostProcessor implements InstantiationAwareBeanPostProcessor {

    Logger logger = LoggerFactory.getLogger(getClass());

    public CustomInstatiationAwareBeanPostProcessor() {
        logger.info("===============InstationAwareBeanPostProcessor构造器===============");
    }

    @Override
    public Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) throws BeansException {
        if (beanClass == Student.class) {
            logger.info("===============InstationAwareBeanPostProcessor.postProcessBeforeInstantiation===============");

        }

        return null;
    }

    @Override
    public boolean postProcessAfterInstantiation(Object bean, String beanName) throws BeansException {
        if (bean instanceof Student) {
            logger.info("===============InstationAwareBeanPostProcessor.postProcessAfterInstantiation===============");
        }
        /**
         * 返回true 会执行postProcessPropertyValues方法
         */
        return true;
    }

    @Override
    public PropertyValues postProcessPropertyValues(PropertyValues pvs, PropertyDescriptor[] pds, Object bean, String beanName) throws BeansException {
        if (bean instanceof Student) {
            logger.info("===============InstationAwareBeanPostProcessor.postProcessPropertyValues,修改名字:{}===============", "Lisi");
            // 修改属性，在执行这个方法之前，bean属性已经有值
           ((Student) bean).setName("Lisi");
            return pvs;

        }
        return pvs;
    }

    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        if (bean instanceof Student) {
//            logger.info("===============CustomInstatiationAwareBeanPostProcessor.postProcessBeforeInitialization==============={}",bean.toString());
        }
        return bean;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        if (bean instanceof Student) {
//            logger.info("===============CustomInstatiationAwareBeanPostProcessor.postProcessAfterInitialization==============={}",bean.toString());
        }
        return bean;
    }
}
